#!/bin/sh
#Author shineflag
#Date 2017-09-13
#Desc 更新现金场的启动脚本

ulimit -c unlimited 

##查找skynet的根目录
if [ ! $SKYNET ]; then  
  echo "can't find skynet path!"
  exit 1  
fi 

export ROOT=$(cd `dirname $0`; pwd)
CFILE="etc/cash_config.lua"  #配置文件
GFILE="cash.group"   #存放当前现金桌组的文件
RPATH=$ROOT/run 
if [ ! -d $RPATH ]; then 
	mkdir "$RPATH"
fi

if [ ! -f $RPATH/$GFILE ]; then 
	RUN_GROUP=1 
else
	RUN_GROUP=$(cat $RPATH/$GFILE)
fi
export CASH_GROUP=$[3-$RUN_GROUP]  #在etc/cash_config.lua 有用到
export PIDFILE=$RPATH/cash$CASH_GROUP.pid

echo "before restart skynet pids are:"
ps -ef | grep skynet | grep -v "grep"

if [ -f $PIDFILE ]; then
	PID=`cat $PIDFILE`
	echo "restart group $CASH_GROUP old pid $PID"
	ret=`kill $PID`
	if [ $? -eq 0 ]; then
		echo "kill old group $CASH_GROUP pid:$PID sucess"
	else
		echo "kill old group $CASH_GROUP pid:$PID failed"
	fi
fi

export DAEMON=true
ret=`$SKYNET/skynet $ROOT/$CFILE`

if [ $? -eq 0 ]; then
	PID=`cat $PIDFILE`
	echo "restart group $CASH_GROUP new pid:$PID sucess"
	echo $CASH_GROUP>$RPATH/$GFILE 
else
	echo "restart group $CASH_GROUP failed"
fi

echo "after restart skynet pids are:"
ps -ef | grep skynet | grep -v "grep"

